import 'package:aihu_getx_app/app/modules/d/d_home/controllers/d_home_controller.dart';
import 'package:aihu_getx_app/app/modules/d/d_workbench/models/auth_permission_info_resp_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_workbench/models/menu_vo.dart';
import 'package:aihu_getx_app/app/modules/d/d_workbench/providers/d_workbench_provider.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DWorkbenchController extends DHomeController {
  final String title = "工作台";
  final scaffoldKey = GlobalKey<ScaffoldState>();
  final permissionInfo = AuthPermissionInfoRespVo().obs;

  final count = 0.obs;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }

  void increment() => count.value++;

  List<Widget> menuList() {
    List<Widget> list = [];
    // if (null != permissionInfo.value.user) {
    //   list.add(
    //     DrawerHeader(
    //       padding: const EdgeInsets.all(0),
    //       margin: const EdgeInsets.all(0),
    //       child: Column(
    //         mainAxisAlignment: MainAxisAlignment.center,
    //         children: [
    //           CircleAvatar(
    //             radius: 40,
    //             backgroundImage: NetworkImage(
    //               permissionInfo.value.user!.avatar ??
    //                   Constants.appBarLogoImage,
    //             ),
    //           ),
    //           Text(
    //             permissionInfo.value.user!.nickname ?? "",
    //             style: const TextStyle(
    //               color: Colors.white,
    //               fontSize: 20,
    //             ),
    //           ),
    //         ],
    //       ),
    //     ),
    //   );
    // }
    if (null != permissionInfo.value.menus) {
      for (var element in permissionInfo.value.menus!) {
        list.add(_buildItem(element));
      }
    }
    return list;
  }

  Widget _buildItem(MenuVo element, {level = 0}) {
    if (element.type == 2) {
      return ListTile(
        leading: SizedBox(
          width: 20.0 * level,
        ),
        title: Text("${element.name}", style: const TextStyle(fontSize: 16)),
        onTap: () {
          Get.find<DHomeController>().pageTitle.value = element.name ?? "";
          Get.toNamed(
            "/d/${element.component}",
            arguments: {
              "pageTitle": element.name,
            },
          );
          closeDrawer();
        },
        textColor: Colors.white,
        iconColor: Colors.white,
        trailing: const Icon(Icons.chevron_right),
      );
    }
    return ExpansionTile(
      title: Text(element.name ?? "",
          style: const TextStyle(color: Colors.white, fontSize: 20)),
      textColor: Colors.white,
      iconColor: Colors.white,
      collapsedIconColor: Colors.white,
      leading: Padding(
        padding: EdgeInsets.only(left: 20.0 * level),
        child: Icon(
            element.isOpen.value ? Icons.expand_more : Icons.chevron_right),
      ),
      onExpansionChanged: (value) {
        element.isOpen.value = value;
      },
      trailing: const Icon(null),
      initiallyExpanded: element.isOpen.value,
      children: element.children
              ?.map((e) => _buildItem(e, level: level + 1))
              .toList() ??
          [],
    );
  }

  void openDrawer() {
    DWorkbenchProvider.getPermissionInfo().then(
      (res) => {
        permissionInfo.value = res,
        scaffoldKey.currentState?.openDrawer(),
      },
      onError: (err) {
        LogUtil.e(err);
      },
    );
  }

  void closeDrawer() {
    scaffoldKey.currentState?.closeDrawer();
  }
}
